<template>
  <view class="scroll-view-container">
    <!-- 侧边栏 -->
    <scroll-view scroll-y
                 class="left-scroll-view"
                 :style="{height:h+'px'}">
      <view v-for="(item,index) in categoryList"
            :key="item.cat_id"
            :class="[currentIndex==index?'active':'','left-scroll-view-item']"
            @click="currentIndex=index">
        <text>{{item.cat_name}}</text>
      </view>
    </scroll-view>
    <!-- 右边栏渲染 -->
    <scroll-view scroll-y
                 class="right-scroll-view"
                 :style="{height:h+'px'}">
      <view v-for="item in levelTwoList "
            :key="item.cat_id"
            class="cate-lv2-title">
        /{{item.cat_name}}/
        <view class="cate-lv3-list">
          <!-- 三级分类 -->
          <view v-for="subitem in item.children"
                :key="subitem.cat_id"
                class="cate-lv3-item ">
            <img :src="subitem.cat_icon"
                 alt=""
                 @click="toGoodList(subitem.cat_id)">
            <text>{{subitem.cat_name}}</text>
          </view>
        </view>
      </view>
      <!-- 三级商品展示 -->

    </scroll-view>
  </view>
</template>
<script>
import setTabBarBridge from '../../mixin/car-tabbar'
export default ({
  mixins: [setTabBarBridge],
  data () {
    return {
      categoryList: [],
      currentIndex: 0,
      // levelTwoList: []
      h: ''

    };
  },
  onLoad (options) {
    this.getCategories()
    // 获取屏幕可用高度
    uni.getSystemInfo({
      success: (res) => {
        console.log(res);
        this.h = res.windowHeight
      }
    })

  },
  // 利用计算属性来获取点击时下标来得到对应的二级数据三级数据
  computed: {
    levelTwoList () {
      return this.categoryList[this.currentIndex].children
    }
  },
  methods: {
    // 点击三级目录跳转到商品详情页展示
    toGoodList (id) {
      uni.navigateTo({
        url: '/subpckg/goods_lists/goods_list?cid=' + id
      })
    },
    async getCategories () {
      let { message, meta: { status } } = await uni.$request({
        url: 'categories'
      })
      if (status === 200) {
        this.categoryList = message
      }
    }

  }


})
</script>
<style lang="scss" scoped>
.scroll-view-container {
  display: flex;

  .left-scroll-view {
    width: 240rpx;

    .left-scroll-view-item {
      line-height: 60px;
      background-color: #f7f7f7;
      text-align: center;
      font-size: 12px;

      // 激活项的样式
      &.active {
        background-color: #ffffff;
        position: relative;

        // 渲染激活项左侧的红色指示边线
        &::before {
          content: " ";
          display: block;
          width: 3px;
          height: 30px;
          background-color: #c00000;
          position: absolute;
          left: 0;
          top: 50%;
          transform: translateY(-50%);
        }
      }
    }
  }

  .right-scroll-view {
    .cate-lv2-title {
      font-size: 12px;
      font-weight: bold;
      text-align: center;
      padding: 15px 0;
    }
    .cate-lv3-list {
      display: flex;
      flex-wrap: wrap;

      .cate-lv3-item {
        width: 33.33%;
        margin-bottom: 10px;
        display: flex;
        flex-direction: column;
        align-items: center;

        image {
          width: 60px;
          height: 60px;
        }

        text {
          font-size: 12px;
        }
      }
    }
  }
}
</style>